/**
 * @file 列表页
 * @module pages/list
 * @author 月落 <yueluo.yang@qq.com>
 */

/**
 * @requires libs/router - 路由跳转
 * @requires config/navbar - 导航配置
 * @requires config/topic - 主题配置
 * @requires models/music - 音乐功能
 * @requires models/story - 故事功能
 * @requires libs/tools - 工具函数
 */
import router from '../../libs/router';
import { list_navbars } from '../../config/navbar';
import { TOPIC } from '../../config/topic';
import musicModel from '../../models/music';
import storyModel from '../../models/story';
import tools from '../../libs/tools';

// 获取数据仓库
const { $store } = getApp();

Page({
  /**
   * @property {string} topic - 当前主题（音乐/故事）
   * @property {object} navbars - 图标导航配置
   * @property {string} title - 名称
   * @property {string} coverImgUrl - 背景图
   * @property {string} tags - 标签
   * @property {array} lists - 列表
   * @property {number} subCount - 订阅数量
   * @property {number} playCount - 播放数量
   */
  data: {
    topic: '',
    navbars: list_navbars,
    title: '',
    coverImgUrl: '',
    tags: '',
    lists: [],
    subCount: 0,
    playCount: 0
  },

  onLoad (options) {
    const { topic } = $store.state;

    this.setData({
      topic
    });

    // 初始化
    this.init(options);
  },

  /**
   * @description 初始化函数
   * @param {object} options - 配置项
   * @returns {void}
   */
  async init (options) {
    const { id } = options,
          { topic, type } = $store.state;
    
    switch (topic) {
      case TOPIC.MUSIC:
        // 获取音乐主题详情
        this.getMusicTopicDetail(id);
        break;
      case TOPIC.STORY:
        // 获取故事主题详情
        this.getStoryTopicDetail(type, id);
        break;
      default:
        break;
    }
  },

  /**
   * @description 获取音乐主题详情
   * @param {number} id - 主题ID
   * @returns {void}
   */
  async getMusicTopicDetail (id) {
    try {
      tools.showLoading();
      const { result } = await musicModel.getTopicDetail(id);
      tools.hideLoading();
  
      const { code, message, data } = result;
  
      if (code === 0) {
        this.setData({
          ...data
        });
        return;
      }
      
      console.log(code, message);
      router.goBack();
    } catch (error) {
      console.log('timeout.');
      router.goBack();
    }
  },

  /**
   * @description 获取故事主题详情
   * @param {string} topic - 主题类型
   * @param {number | string} id - 主题ID
   * @returns {void} 
   */
  async getStoryTopicDetail (topic, id) {
    try {
      tools.showLoading();
      const { result } = await storyModel.getTopicDetail(topic, id);
      tools.hideLoading();
  
      const { code, message, data } = result;
  
      if (code === 0) {
        this.setData({
          ...data
        });
        return;
      }
      
      console.log(code, message);
      router.goBack();
    } catch (error) {
      console.log('timeout.');
      router.goBack();
    }
  },

  /**
   * @description 导航栏点击事件处理
   * @param {object} e - 事件源
   * @returns {void} 
   */
  onIconBtnClick (e) {
    const { action } = e.currentTarget.dataset;

    switch (action) {
      case 'back':
        router.goBack();
        break;
      case 'collect':
        break;
      default:
        console.log('no matching entries.');
        break;
    }
  },

  /**
   * @description 跳转至播放页
   * @param {object} e - 事件源
   * @returns {void} 
   */
  onMusicClick (e) {
    const { index } = e.currentTarget.dataset;

    switch (this.data.topic) {
      case TOPIC.MUSIC:
        const { id, name, author, url } = this.data.lists[index];
        // 提交数据
        $store.commit('changeState', {
          playParams: { id, title: name, author, imgUrl: url }
        });
        break;
      case TOPIC.STORY:
        const { id: _id, title, text } = this.data.lists[index],
              { coverImgUrl } = this.data;
        // 提交数据
        $store.commit('changeState', {
          playParams: { id: _id, title: title, text, imgUrl: coverImgUrl }
        });
        break;
      default:
        break;
    }

    // 跳转至播放页
    router.navigate(`/pages/play/play`);
  }
})